package cc.market.controller.user;


import cc.market.VO.ProductVO;
import cc.market.constant.MessageConstant;
import cc.market.entity.ProductInfo;
import cc.market.entity.User;
import cc.market.result.Result;
import cc.market.service.ProductService;
import cc.market.entity.ProductCategory;
import cc.market.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/user/product")
@Slf4j
@Tag(name = "商品相关接口")
public class productController {

    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;

    @GetMapping("/getcategory")
    @Operation(summary = "获取商品分类")
    public Result<List<ProductCategory>> getCategoryList(){
        List<ProductCategory> list =  productService.get();
        return Result.success(MessageConstant.REQUEST_SUCCESS,list);
    }

    @PostMapping("/publish")
    @Operation(summary = "上传商品信息")
    public Result publish(@RequestBody ProductInfo productInfo){
        productService.insert(productInfo);
        return Result.success(MessageConstant.PUBLISH_SUCCESS);
    }

    @GetMapping("/getbycategoryid")
    @Operation(summary = "根据分类查询商品信息")
    public Result<List<ProductVO>> getProductById(@RequestParam(required = false) Integer categoryId){
        List<ProductInfo> prodlist = productService.getByCategoryId(categoryId);
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductInfo productInfo : prodlist) {
            ProductVO productVO = new ProductVO();
            // 逐个对象拷贝属性
            BeanUtils.copyProperties(productInfo, productVO);
            /* 根据每个商品中的userId属性查询user表 */
            User user = userService.getById(productInfo.getUserId());
            productVO.setAvatar(user.getAvatar());
            productVO.setNickName(user.getNickName());
            // 将拷贝后的 VO 对象添加到列表中
            productVOList.add(productVO);
        }
        return Result.success(MessageConstant.SEARCH_SUCCESS,productVOList);
    }

    @GetMapping("/getproductbylocation")
    @Operation(summary = "根据定位查询商品信息")
    public Result<List<ProductVO>> getProductByLocation(@RequestParam String province,String city){
        List<ProductInfo> prodlist = productService.getByLocation(province,city);
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductInfo productInfo : prodlist) {
            ProductVO productVO = new ProductVO();
            // 逐个对象拷贝属性
            BeanUtils.copyProperties(productInfo, productVO);
            /* 根据每个商品中的userId属性查询user表 */
            User user = userService.getById(productInfo.getUserId());
            productVO.setAvatar(user.getAvatar());
            productVO.setNickName(user.getNickName());
            // 将拷贝后的 VO 对象添加到列表中
            productVOList.add(productVO);
        }
        return Result.success(MessageConstant.SEARCH_SUCCESS,productVOList);
    }

    @GetMapping("/getproductdetail")
    @Operation(summary = "根据id查询商品信息")
    public Result<ProductInfo> getById(@RequestParam Integer id){
        ProductInfo productInfo =  productService.getById(id);
        if (productInfo == null){
            return Result.error(MessageConstant.SEARCH_ERROR);
        }
        return Result.success(MessageConstant.SEARCH_SUCCESS,productInfo);
    }
}
